package com.boluo.businessManagement.service.impl;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

import com.boluo.businessManagement.domain.BusMentContractConst;
import com.boluo.businessManagement.dto.BusMentContractDTO;
import com.boluo.common.utils.DateUtils;
import com.boluo.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.boluo.businessManagement.mapper.BusMentContractMapper;
import com.boluo.businessManagement.domain.BusMentContract;
import com.boluo.businessManagement.service.IBusMentContractService;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 合同信息Service业务层处理
 * 
 * @author xlk
 * @date 2023-05-02
 */
@Service
public class BusMentContractServiceImpl implements IBusMentContractService 
{
    @Resource
    private BusMentContractMapper busMentContractMapper;
    @Resource
    private HttpServletResponse response;

    /**
     * 查询合同信息
     * 
     * @param id 合同信息主键
     * @return 合同信息
     */
    @Override
    public BusMentContract selectBusMentContractById(Long id)
    {
        return busMentContractMapper.selectBusMentContractById(id);
    }

    /**
     * 查询合同信息列表
     * 
     * @param
     * @return 合同信息
     */
    @Override
    public List<BusMentContract> selectBusMentContractList(BusMentContractDTO busMentContractDTO)
    {
        return busMentContractMapper.selectBusMentContractList(busMentContractDTO);
    }

    /**
     * 新增合同信息
     * 
     * @param busMentContract 合同信息
     * @return 结果
     */
    @Override
    public int insertBusMentContract(BusMentContract busMentContract)
    {
        Assert.notNull(busMentContract.getCustomerId(),"所属客户不能为空");
        Assert.notNull(busMentContract.getContractName(),"所合同名称不能为空");
        Assert.notNull(busMentContract.getContractCode(),"合同编码不能为空");
        Assert.notNull(busMentContract.getAmounts(),"合同金额不能为空");
        Assert.notNull(busMentContract.getStartDate(),"合同生效时间不能为空");
        Assert.notNull(busMentContract.getEndDate(),"合同失效时间不能为空");
        Assert.notNull(busMentContract.getAppendix(),"合同文件不能为空");
        Assert.state(busMentContract.getContractName().length()<100,"字数请在100字内");
        Assert.state(busMentContract.getContractCode().length()<50,"合同编码请少于50字");
        Assert.state(busMentContract.getAppendix().length()!=0,"请上传附件");
        Assert.state(busMentContract.getAmounts().compareTo(new BigDecimal("0.0"))>0,"请输入正数");
        busMentContract.setInputTime(DateUtils.getNowDate());
        busMentContract.setInputUser(SecurityUtils.getUserId());
        busMentContract.setAffixSealState(BusMentContractConst.CONTRACT_SEAL_NO);
        busMentContract.setAuditState(BusMentContractConst.CONTRACT_AUDIT_INIT);
        busMentContract.setNullifyState(BusMentContractConst.CONTRACT_EFFECTIVE);
        return busMentContractMapper.insertBusMentContract(busMentContract);
    }

    /**
     * 修改合同信息
     * 
     * @param busMentContract 合同信息
     * @return 结果
     */
    @Override
    public int updateBusMentContract(BusMentContract busMentContract)
    {
        Assert.notNull(busMentContract.getId(),"合同id为空,数据异常");
        Assert.notNull(busMentContract,"合同信息为空,数据异常");
        Assert.state(busMentContract.getAuditState()!=BusMentContractConst.CONTRACT_AUDIT_YES,"只有未审核的合同才能修改");
        Assert.state(busMentContract.getAuditState()!=BusMentContractConst.CONTRACT_AUDIT_NO,"只有未审核的合同才能修改");
        return busMentContractMapper.updateBusMentContract(busMentContract);
    }

    /**
     * 批量删除合同信息
     * 
     * @param ids 需要删除的合同信息主键
     * @return 结果
     */
    @Override
    public int deleteBusMentContractByIds(Long[] ids)
    {
        return busMentContractMapper.deleteBusMentContractByIds(ids);
    }

    /**
     * 删除合同信息信息
     * 
     * @param id 合同信息主键
     * @return 结果
     */
    @Override
    public int deleteBusMentContractById(Long id)
    {
        return busMentContractMapper.deleteBusMentContractById(id);
    }

    @Override
    public int auditYes(Long id) {
        Assert.notNull(id,"合同id为空,数据异常");
        BusMentContract busMentContract = busMentContractMapper.selectBusMentContractById(id);
        Assert.notNull(busMentContract,"合同信息为空,数据异常");
        Assert.state(busMentContract.getAuditState()!=2,"该合同未通过,无法再次通过");
        busMentContract.setAuditState(BusMentContractConst.CONTRACT_AUDIT_YES);
        return busMentContractMapper.updateBusMentContract(busMentContract);
    }

    @Override
    public int auditNo(Long id) {
        Assert.notNull(id,"合同id为空,数据异常");
        BusMentContract busMentContract = busMentContractMapper.selectBusMentContractById(id);
        Assert.notNull(busMentContract,"合同信息为空,数据异常");
        Assert.state(busMentContract.getAuditState()!=BusMentContractConst.CONTRACT_AUDIT_NO,"该合同未通过,无法再次通过");
        busMentContract.setAuditState(BusMentContractConst.CONTRACT_AUDIT_NO);
        busMentContract.setNullifyState(BusMentContractConst.CONTRACT_INVALID);
        return busMentContractMapper.updateBusMentContract(busMentContract);
    }

    @Override
    public int sealYes(Long id) {
        Assert.notNull(id,"合同id为空,数据异常");
        BusMentContract busMentContract = busMentContractMapper.selectBusMentContractById(id);
        Assert.notNull(busMentContract,"合同信息为空,数据异常");
        Assert.state(busMentContract.getAffixSealState()!=BusMentContractConst.CONTRACT_SEAL_YES,"该合同已经盖过章");
        Assert.state(busMentContract.getAuditState()!=BusMentContractConst.CONTRACT_AUDIT_INIT,"未审核的合同无法盖章");
        Assert.state(busMentContract.getAuditState()!=BusMentContractConst.CONTRACT_AUDIT_NO,"审核不通过的合同无法盖章");
        busMentContract.setAffixSealState(BusMentContractConst.CONTRACT_SEAL_YES);
        return busMentContractMapper.updateBusMentContract(busMentContract);
    }

    @Override
    public int InvalidYes(Long id) {
        Assert.notNull(id,"合同id为空,数据异常");
        BusMentContract busMentContract = busMentContractMapper.selectBusMentContractById(id);
        Assert.notNull(busMentContract,"合同信息为空,数据异常");
        Assert.state(busMentContract.getNullifyState()!=BusMentContractConst.CONTRACT_INVALID,"该合同已作废,操作失败");
        Assert.state(busMentContract.getAffixSealState()!=BusMentContractConst.CONTRACT_SEAL_YES,"该合同已经盖章,无法作废");
        busMentContract.setNullifyState(BusMentContractConst.CONTRACT_INVALID);
        return busMentContractMapper.updateBusMentContract(busMentContract);
    }

    @Override
    public int downloadAnnex(Long id) throws IOException {
//        BusMentContract busMentContract = busMentContractMapper.selectBusMentContractByAppendix(appendix);
//        new BufferedOutputStream()
        BusMentContract busMentContract = busMentContractMapper.selectBusMentContractById(id);
        String appendix = busMentContract.getAppendix();
        Files.copy(Paths.get(appendix),response.getOutputStream());
        return 1;
    }
}
